package pt.inescid.components.policyengine.heimdall.base;

import java.util.Date;
import java.util.Random;
import java.util.Vector;

import pt.inescid.components.policyengine.heimdall.database.EventObjectDAO;
import pt.inescid.components.policyengine.heimdall.database.PolicyDAO;
import pt.inescid.components.policyengine.heimdall.pap.base.ModelDefinition;
import pt.inescid.components.policyengine.heimdall.pdp.base.ModelInstance;

public class User {
	
	private static Random randomGenerator = null;
	private static boolean randomInitialized = false;
	private static final int MODEL_TAG_SIZE = 3;
	
	private long id;
	private String name;
	private Vector models;
	
	public User(long id, String name) {
		super();
		this.id = id;
		this.name = name;
		this.models = new Vector();
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}
	

	public ModelDefinition getModel(int modelIndex) {
		return ((ModelDefinition) models.elementAt(modelIndex));
	}


	
	/* ===========================================================
	 ==============================================================
	 =============                    =============================
	 =============      getModelByTag =========================
	 ==========================================
	 ==============================================================
	 =========================================================== */
	public ModelDefinition getModelByTag(String modelTag) {
		ModelDefinition currentModel = null;
		for (int i = 0; i < models.size(); i++) {
			currentModel = (ModelDefinition) models.elementAt(i);
			if (currentModel.getTag().equals(modelTag)) {
				return (currentModel);
			}
		}
		return (null);
	} // getModelByTag

	
	/* ===========================================================
	 ==============================================================
	 =============                    =============================
	 =============      getModelEvents =========================
	 ==========================================
	 ==============================================================
	 =========================================================== */
	public Vector getModelEvents(String modelTag) {
		return (EventObjectDAO.getAllRecords(this, modelTag));
	} // getModelByTag

	/* ===========================================================
	 ==============================================================
	 =============                    =============================
	 =============      getRelevantModels =========================
	 ==========================================
	 ==============================================================
	 =========================================================== */
	public Vector getRelevantModels(String modelTag) {
		Vector result = new Vector();
		
		ModelDefinition currentModel = null;
		for (int i = 0; i < models.size(); i++) {
			currentModel = (ModelDefinition) models.elementAt(i);
			if (currentModel.getTag().contains(modelTag)) {
				result.add(currentModel);
			}
		}
		return (result);
	} // getRelevantModels

	/* ===========================================================
	 ==============================================================
	 =============                    =============================
	 =============      getCesModel =========================
	 ==========================================
	 ==============================================================
	 =========================================================== */
	public ModelDefinition getCesModel(String modelTag, String cesName) {
		ModelDefinition currentModel = null;
		for (int i = 0; i < models.size(); i++) {
			currentModel = (ModelDefinition) models.elementAt(i);
			if (currentModel.getTag().equals(modelTag + "_" + cesName)) {
				return (currentModel);
			}
		}
		return (null);
	} // getModelByTag

	
	public ModelInstance getModelInstance(EventObj ce, String modelTag) {
		return (getModelByTag(modelTag).getEvaluationInstance(this, ce));
	}

	public void addModel(ModelDefinition model) {
		this.models.add(model);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	/* ===========================================================
	==============================================================
	==============================================================
	getNewModelTag
	==============================================================
	==============================================================
	=========================================================== */
	public String getNewModelTag() {
		long seed = 0;
		Vector allRecords = PolicyDAO.getAllRecords();
		int numberOfRecords = 0;
		
		if (allRecords != null && allRecords.size() != 0) {
			numberOfRecords = allRecords.size();
			seed = ((long) numberOfRecords) * (new Date().getTime());
		}


		if (!randomInitialized) {
			randomGenerator = new Random(seed);
			randomInitialized = true;
		}
		
		byte token[] = new byte[MODEL_TAG_SIZE];
		randomGenerator.nextBytes(token);
		
		final int characterRange = 'Z' - 'A' + 1;
		for (int i = 0; i < MODEL_TAG_SIZE; i++) {
			token[i] %= characterRange;
			if (token[i] < 0) {
				token[i] = (byte) -token[i];
			}
			token[i] += 'A';
		}
		
		return new String(token);
	} // getNewModelTag

	
	
	public Vector getModels() {
		return models;
	}

	
	/* ===========================================================
	==============================================================
	==============================================================
	toString
	==============================================================
	==============================================================
	=========================================================== */
	public String toString() {
		return ("" + id + "." + name);
	}
}